package com.apksources.torch;

import com.bwf.example.light.R;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.Camera;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

public class TorchService extends Service {

	public static final String SWITCH = "switch";
	public static final int SWITCH_ON = 0x01;
	public static final int SWITCH_OFF = 0x02;

	private static final String TAG = "TorchService";
	private static TorchService mInstance;
	private boolean isTorchTurnOn;
	private Camera mCamera;
	private Camera.Parameters mParameters;
	private NotificationManager mNotificationManager;
	private Notification mNotification;

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	@Override
	public void onCreate() {
		TorchService.mInstance = TorchService.this;
		this.mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
		super.onCreate();
	}

	@Override
	public void onDestroy() {
		if (this.mCamera != null) {
			this.mCamera.stopPreview();
			this.mCamera.release();
		}
		if (this.mNotificationManager != null) {
			this.mNotificationManager.cancelAll();
		}
		TorchService.mInstance = null;
		super.onDestroy();
	}

	@SuppressWarnings("deprecation")
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.d(TAG, "TorchService --> onStartCommand");
		if (intent != null) {

			switch (intent.getIntExtra(SWITCH, -1)) {
				case SWITCH_ON: {
					try {
						// 创建Camera对象
						this.mCamera = Camera.open();

						if (Build.VERSION.SDK_INT < 7) {
							this.stopSelf();
						} else {
							
							this.mParameters = mCamera.getParameters();
							this.mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
							this.mCamera.setParameters(this.mParameters);
							
							MySurfaceView mySurfaceView = new MySurfaceView(TorchService.this);
							mySurfaceView.mCamera = mCamera;
							MainActivity.mInstance.addSurfaceView(mySurfaceView);

							

							this.mNotification = new Notification(R.drawable.notification_icon, "手电筒", System.currentTimeMillis());
							this.mNotification.setLatestEventInfo(this, "手电筒", "点击进入手电筒",
									PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0));

							this.mNotificationManager.notify(0, this.mNotification);

							this.startForeground(0, this.mNotification);
						}
						this.isTorchTurnOn = true;

					} catch (RuntimeException e) {
						e.printStackTrace();
					}
					break;
				}
				case SWITCH_OFF: {
					this.stopSelf();
					break;
				}
			}

		}

		return START_STICKY;
	}

	/**
	 * 返回闪光灯是否打开
	 * 
	 * @return true：打开；false：关闭
	 */
	public static boolean isTorchTurnedOn() {
		if (mInstance != null) {
			return mInstance.isTorchTurnOn;
		}
		return false;
	}

}
